﻿using Resume.IRepository;
using Resume.Model;
using Resume.ModelMigration;
using System.Linq.Expressions;

namespace Resume.Repository;

/// <summary>
/// 指标管理
/// </summary>
public class FiledSetting_Repository : IFieldSetting_Repository
{
    private static readonly XLZFContext context = new();

    private readonly IRepository<FieldSettingDict> _repository = new Repository<FieldSettingDict>(context);

    private readonly IRepository<FieldClassDict> _repositoryClass = new Repository<FieldClassDict>(context);

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="FieldSettingDict"></param>
    /// <returns></returns>
    public int Add(FieldSettingDict FieldSettingDict)
    {
        return _repository.Add(FieldSettingDict);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="FieldSettingDict"></param>
    /// <returns></returns>
    public int Del(FieldSettingDict FieldSettingDict)
    {
        return _repository.Remove(FieldSettingDict);
    }

    /// <summary>
    /// 列表
    /// </summary>
    /// <returns></returns>
    public List<FieldSettingDict> Get(int id)
    {
        var obj = _repositoryClass.GetModelById(id);

        return _repository.GetAll(p => p.FieldClassCode == obj.FieldClassCode).ToList();
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="name"></param>
    /// <param name="isAsc"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public Page<FieldSettingDict> GetPageData(string name, bool isAsc, int pageIndex, int pageSize)
    {
        Page<FieldSettingDict>? data = null;

        if (string.IsNullOrEmpty(name))
        {
            data = _repository.SearchFor(pageIndex, pageSize, null, isAsc, e => e.CreateDate);
        }
        else
        {
            //条件表达式
            Expression<Func<FieldSettingDict, bool>> predicate = e => string.IsNullOrWhiteSpace(name) || e.FieldName.Equals(name);

            data = _repository.SearchFor(pageIndex, pageSize, predicate, isAsc, e => e.CreateDate);
        }

        return data;
    }

    /// <summary>
    /// 获取单个数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public FieldSettingDict GetSinge(int id)
    {
        return _repository.GetModel(p => p.Id == id);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="FieldSettingDict"></param>
    /// <returns></returns>
    public int Edit(FieldSettingDict FieldSettingDict)
    {
        return _repository.Update(FieldSettingDict);
    }
}
